package com.example.t10v26;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;

public class HelloController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private Button addCandyBtn;

    @FXML
    private TextField amoungMoneyTF;

    @FXML
    private TextField candyAddTF;

    @FXML
    private ListView<String> inputCandyList;

    @FXML
    private ListView<String> outputCandyList;

    @FXML
    private Button solveBtn;
    private List<Candy> li = new ArrayList<Candy>();

    @FXML
    void addCandyClick(ActionEvent event) {
        Candy candy;
        String[] splited = candyAddTF.getText().split("-");
        candy = new Candy(splited[0], Integer.parseInt(splited[1]));
        li.add(candy);
        inputCandyList.getItems().add(candy.name + "-" + candy.price);
        candyAddTF.setText("");
    }

    @FXML
    void solveClick(ActionEvent event) {
        for(int i = 0; i < li.size();i++){
            Candy c = li.get(i);
            c.amount = 0;
        }
        outputCandyList.getItems().clear();
        int money = Integer.parseInt(amoungMoneyTF.getText());
        Solution sol = new Solution(li);
        List<Candy> res = sol.solve(money);
        for (var i:res
             ) {
            outputCandyList.getItems().add(i.name +"-"+ i.amount);
        }
        amoungMoneyTF.setText(String.valueOf(sol.remaining));
    }

    @FXML
    void initialize() {
        assert addCandyBtn != null : "fx:id=\"addCandyBtn\" was not injected: check your FXML file 'hello-view.fxml'.";
        assert amoungMoneyTF != null : "fx:id=\"amoungMoneyTF\" was not injected: check your FXML file 'hello-view.fxml'.";
        assert candyAddTF != null : "fx:id=\"candyAddTF\" was not injected: check your FXML file 'hello-view.fxml'.";
        assert inputCandyList != null : "fx:id=\"inputCandyList\" was not injected: check your FXML file 'hello-view.fxml'.";
        assert outputCandyList != null : "fx:id=\"outputCandyList\" was not injected: check your FXML file 'hello-view.fxml'.";
        assert solveBtn != null : "fx:id=\"solveBtn\" was not injected: check your FXML file 'hello-view.fxml'.";

    }

}